import 'package:flutter/material.dart';
import '../models/list_item.dart';
import '../widgets/list_item_card_no_provider.dart';

class IconListScreenNoProvider extends StatefulWidget {
  const IconListScreenNoProvider({Key? key}) :super(key: key);

  @override
  State<IconListScreenNoProvider> createState() => _IconListScreenNoProviderState();
}

class _IconListScreenNoProviderState extends State<IconListScreenNoProvider> {
  final List<ListItem> _items = [
    ListItem("邮件", Icons.email, Colors.blue),
    ListItem("日历", Icons.calendar_today, Colors.green),
    ListItem("云盘", Icons.cloud, Colors.orange),
    ListItem("设置", Icons.settings, Colors.purple),
    ListItem("消息", Icons.message, Colors.red),
    ListItem("音乐", Icons.music_note, Colors.teal),
    ListItem("地图", Icons.map, Colors.brown),
    ListItem("购物车", Icons.shopping_cart, Colors.deepOrange),
  ];

  void _addItem() {
    setState(() {
      final newIndex = _items.length + 1;
      _items.add(ListItem("新项目 $newIndex", Icons.new_releases,
        Colors.primaries[newIndex % Colors.primaries.length],
      ));
    });
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text("已添加新项目")),
    );
  }

  void _handleItemTap(int index) {
    ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text("选中：${_items[index].title}")));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: const Text("带图标的列表") ,
          actions :
          [
            IconButton   (
              icon:const  Icon
                (
                  Icons
                      .
                  refresh
              )
              ,
              onPressed
                  :
                  () {
                setState(() => _items.shuffle());
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text("列表已刷新")),
                );
              },)
          ]),
      body: ListView.builder(
        itemCount: _items.length,
        itemBuilder: (context, index) =>
            ListItemCardNoProvider(
              item: _items[index],
              index: index,
              onTap: () => _handleItemTap(index),
            ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _addItem,
        child: const Icon(Icons.add),
      ),);
  }
}